package titt.head.request.form;

import titt.TITTValues;
import titt.head.Content;
import titt.head.ContentHead;
import titt.head.HTTPHead;
import titt.head.request.Request;
import titt.head.request.RequestHead;
import titt.head.util.AbstractRequest;
import titt.util.Bytes;

public class FormHead extends AbstractRequest implements ContentHead, RequestHead {

	private String boundary;
	
	public FormHead(RequestHead req) {
		super.h = req;
		boundary = getHeader("Content-Type").getValue().split("boundary=")[1].trim();
		boundary = boundary.split(";")[0].trim();
	}
	
	public FormHead(int contentlength, String boundary, HTTPHead h) {
		this(new Request(new Content(contentlength, "multipart/form-data;boundary="+boundary, h)));
	}
	
	public byte[][] getBounds() {
		return new byte[][] {
			Bytes.asBytes(TITTValues.MULTIPART_SEPERATOR + boundary),
			Bytes.asBytes(TITTValues.MULTIPART_SEPERATOR + boundary + TITTValues.MULTIPART_SEPERATOR)
		};
	}
	
	public String getBoundary() {
		return boundary;
	}
	
	@Override
	public int getContentLength() {
		return Integer.parseInt(getHeader("Content-Length").getValue());
	}

	@Override
	public boolean containsContentLength() {
		return getHeader("Content-Length") != null;
	}

}
